# 前端构建镜像
FROM node:18-alpine AS frontend
# 设置工作目录
WORKDIR /app
# 拷贝package.json到工作目录下
COPY package*.json ./
COPY *.config.js ./

RUN npm install
# 拷贝前端代码到app目录下
COPY . .
# 构建前端代码
RUN npm run build -- --emptyOutDir && \
    rm -rf node_modules && \
    npm cache clean --force

# 使用nginx作为生产环境
FROM nginx:alpine AS production

# 删除默认配置
RUN rm /etc/nginx/conf.d/default.conf
# 从前端构建的dist目录拷贝到nginx的html目录下

COPY --from=frontend /app/build /usr/share/nginx/html
# 复制nginx的配置文件
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
# 复制证书文件
COPY nginx/certs/*.ctbloge.top.* /etc/nginx/certs/

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
